#!/bin/sh
# autoheader -- create `config.h.in' from `configure.in'
# Copyright (C) 1992, 1993 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Written by Roland McGrath.

# If given no args, create `config.h.in' from template file `configure.in'.
# With one arg, create a header file on standard output from
# the given template file.

usage="Usage: autoheader [-h] [--help] [-m dir] [--macrodir=dir] 
                  [-v] [--version] [template-file]" 

test -z "${AC_MACRODIR}" && AC_MACRODIR=@datadir@
test -z "${M4}" && M4=@M4@

print_version=""
while test $# -gt 0 ; do
   case "z${1}" in 
      z-h | z--help | z--h* )
         echo "${usage}" 1>&2; exit 1 ;;
      z--macrodir=* | z--m*=* )
         AC_MACRODIR="`echo \"${1}\" | sed -e 's/^[^=]*=//'`"
         shift ;;
      z-m | z--macrodir | z--m* ) 
         shift
         test $# -eq 0 && { echo "${usage}" 1>&2; exit 1; }
         AC_MACRODIR="${1}"
         shift ;;
      z-v | z--version | z--v* )
         print_version="-DAC_PRINT_VERSION"
         shift ;;
      z-- )     # Stop option processing
        shift; break ;;
      z- )	# Use stdin as input.
        break ;;
      z-* )
        echo "${usage}" 1>&2; exit 1 ;;
      * )
        break ;;
   esac
done

# Needed for some versions of `tr' so that character classes in `[]' work.
if test "${LANG+set}" = "set" ; then
   LANG=C
fi

TEMPLATES="${AC_MACRODIR}/acconfig.h"
test -r acconfig.h && TEMPLATES="${TEMPLATES} acconfig.h"
MACROFILES="${AC_MACRODIR}/acgeneral.m4 ${AC_MACRODIR}/acspecific.m4"
test -r ${AC_MACRODIR}/aclocal.m4 \
   && MACROFILES="${MACROFILES} ${AC_MACRODIR}/aclocal.m4"
test -r aclocal.m4 && MACROFILES="${MACROFILES} aclocal.m4"
MACROFILES="${print_version} ${MACROFILES}"

case $# in
  0) infile=configure.in ;;
  1) infile=$1 ;;
  *) echo "$usage" >&2; exit 1 ;;
esac

# These are the alternate definitions of the acgeneral.m4 macros we want to
# redefine.  They produce strings in the output marked with "@@@" so we can
# easily extract the information we want.
frob='define([AC_DEFINE],[
@@@syms="$syms $1"@@@
])dnl
define([AC_HAVE_FUNCS],[
@@@funcs="$funcs $1"@@@
])dnl
define([AC_HAVE_HEADERS],[
@@@headers="$headers $1"@@@
])dnl'

# We extract assignments of SYMS, FUNCS, and HEADERS from the modified
# autoconf processing of the input file.
# Traditional sed mishandles /p1/,/p2/ construct if both patterns
# appear on the same line (junio@twinsun.com).
eval "`echo \"$frob\" \
       | $M4 $MACROFILES - $infile \
       | sed -n -e '
		: again
		/^@@@.*@@@$/s/@@@\(.*\)@@@$/\1/p
		/^@@@/{
			s/^@@@//p
			n
			s/^/@@@/
			b again
		}'`"

# Make SYMS newline-separated rather than blank-separated.
syms="`for sym in $syms; do echo $sym; done`"

if test $# -eq 0; then
  tmpout=autoh$$
  trap "rm -f $tmpout; exit 1" 1 3 15
  exec > $tmpout
fi

echo "/* Generated automatically from $infile by autoheader.  DO NOT EDIT!  */"

# This puts each paragraph on its own line, separated by @s.
if test -n "$syms"; then
   # Make sure the boundary of template files is also the boundary
   # of the paragraph.  Extra newlines don't hurt since they will
   # be removed.
   for t in $TEMPLATES; do cat $t; echo; echo; done |
   # The sed script is suboptimal because it has to take care of
   # some broken sed (e.g. AIX) that remove '\n' from the
   # pattern/hold space if the line is empty. (junio@twinsun.com).
   sed -n -e '
	/^[ 	]*$/{
		x
		s/\n/@/g
		p
		s/.*/@/
		x
	}
	H' | sed -e 's/@@*/@/g' |
   # Select each paragraph that refers to a symbol we picked out above.
   fgrep "$syms" |
   tr @ \\012
fi

for func in $funcs; do
  sym="`echo ${func} | sed 's/[^a-zA-Z0-9_]/_/g' | tr '[a-z]' '[A-Z]'`"
  echo "
/* Define if you have ${func}.  */
#undef HAVE_${sym}"
done

for header in $headers; do
  sym="`echo ${header} | sed 's/[^a-zA-Z0-9_]/_/g' | tr '[a-z]' '[A-Z]'`"
  echo "
/* Define if you have the <${header}> header file.  */
#undef HAVE_${sym}"
done

status=0

for sym in $syms; do
  if fgrep $sym $TEMPLATES >/dev/null; then
    : # All is well.
  else
    echo "$0: Symbol \`${sym}' is not covered by $TEMPLATES" >&2
    status=1
  fi
done

if test $# -eq 0; then
  if test $status -eq 0; then
    mv -f $tmpout config.h.in
  else
    rm -f $tmpout
  fi
fi

exit $status
